import 'package:flutter_ncsjqz_getx/util/toast_util.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtil{

  /// todo：单一权限
  static Future<bool> requestPermissionOne(Permission permission) async {
    //获取当前的权限
    var status = await permission.status;
    if (status == PermissionStatus.granted)  return true;  //已经授权
    status = await permission.request();//未授权则发起一次申请
    if (status == PermissionStatus.granted) return true;
    return false;
  }

  /// todo：获取定位权限 false = 无权限、未打开开关
  static Future<bool> getLocation() async{
    bool isShown = await Permission.contacts.shouldShowRequestRationale;
    print(isShown);
    if (await requestPermissionOne(Permission.location)) {
      // 判断是否打开gps开关
      if(await Permission.location.serviceStatus == ServiceStatus.enabled){
        return true;
      }else{
        toast("位置开关未打开，请检查");
        return false;
      }
    }else{
      return false;
    }
  }

  /// todo：打开设置页面 false = 未打开 ， true = 已打开
  static Future<bool> openSetting() async{
    return await openAppSettings();
  }
}